package com.brand.prisd.ViewHolder;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.brand.prisd.R;
import com.brand.prisd.fragment.AddDeviceFragment;
import com.ogaclejapan.smarttablayout.SmartTabLayout;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by HJ on 2019/1/4.
 */
public class Type9ViewHolder extends RecyclerView.ViewHolder {
    @BindView(R.id.stl)
    SmartTabLayout stl;
    @BindView(R.id.vp)
    ViewPager vp;
    private Context context;

    public Type9ViewHolder(@NonNull View itemView) {
        super(itemView);
        ButterKnife.bind(this, itemView);
        context = itemView.getContext();
        initData();
    }

    private String[] tabs;
    private Fragment[] fragments;

    private void initData() {
        tabs = new String[]{"所有设备", "客厅", "主卧", "次卧", "餐厅", "厨房", "书房"};
        fragments = new Fragment[tabs.length];
    }

    private int currentPosition;//记录当前ViewPager显示的item位置
    private int lastPosition = 0;//记录上一次ViewPager显示的item位置，默认为0。

    public void getFragmentManger(FragmentManager childFragmentManager) {
        ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(childFragmentManager);
        vp.setAdapter(viewPagerAdapter);
        stl.setViewPager(vp);
        if (vp.getCurrentItem() == 0) {
            TextView tabAt = (TextView) stl.getTabAt(0);
            tabAt.setTextSize(20);
            tabAt.setTextColor(Color.BLACK);
        }
        stl.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                currentPosition = i;
                if (currentPosition != lastPosition ) {
                    TextView tabAt = (TextView) stl.getTabAt(i);
                    tabAt.setTextSize(20);
                    tabAt.setTextColor(Color.BLACK);
                }
                if (currentPosition != lastPosition && lastPosition>=0){
                    TextView tabAt = (TextView) stl.getTabAt(lastPosition);
                    tabAt.setTextSize(15);
                    tabAt.setTextColor(Color.GRAY);
                }
                lastPosition = currentPosition;
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
    }

    class ViewPagerAdapter extends FragmentPagerAdapter {

        ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Nullable
        @Override
        public CharSequence getPageTitle(int position) {

            return tabs[position];
        }


        @Override
        public Fragment getItem(int i) {

            return fragments[i] = new AddDeviceFragment();
        }

        @Override
        public int getCount() {
            return tabs.length;
        }
    }
}
